/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commande;

import entity.Produit;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author livetnediser
 */
public class Command {
    public List<CommandItem> items;
    public double total;
    int numberOfItems;
    
    public Command(){
        items = new ArrayList<CommandItem>();
        numberOfItems = 0;
        total = 0;
    }
    
    public synchronized void addItem(Produit product, double price, int quantity, String name) {

        boolean newItem = true;

        for (CommandItem scItem : items) {
            if (scItem.getProduct().getIdproduit() == product.getIdproduit()) {
                newItem = false;
            }
        }
        if (newItem) {
            CommandItem scItem = new CommandItem(product, price, quantity);
            scItem.setName(name);
            items.add(scItem);
        }
    }
    
    public synchronized List<CommandItem> getItems() {
        return items;
    }
    
    public synchronized int getNumberOfItems() {

        numberOfItems = 0;

        for (CommandItem scItem : items) {

            numberOfItems += scItem.getQuantity();
        }

        return numberOfItems;
    }
    
    public synchronized double getTotal() {

        total = 0;

        for (CommandItem scItem : items) {
            total += (scItem.getTotal());
        }
        total = Math.rint(total*100)/100;

        return total;
    }
    
    public synchronized void clear() {
        items.clear();
        numberOfItems = 0;
        total = 0;
    }
}
